<?php if ($topic->open != 0): ?>
	<script>
		function submit_message_form() {
			if ($('#message_form input[name=preview]').val() == 1) {
				return true;
			} else {
				$('#message_form_ajax').css('visibility', 'visible');
				$.post(
					'<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'ajax_add_message'))); ?>',
					$('#message_form').serialize(),
					function(data) {
						if (data.status == 'error') {
							$('#message_form_msgbox').html('<div class="msg msg-error">'+data.message+'</div>');
							$('#message_form_ajax').css('visibility', 'hidden');
						} else {
							var redirect = '<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'goto', 'goto' => 'topic', 'id' => '{message_id}'))); ?>';
							redirect = redirect.replace('{message_id}', data.message_id);
							setTimeout(function() {
								window.location.href = mcmf_route_redirect.replace('{url}', base64_encode(redirect));
							}, mcmf_timeout_default);
							$('#message_form_msgbox').html('<div class="msg msg-success">Сообщение добавлено. Перенаправление...</div>');
							$('#message_form_ajax').css('visibility', 'hidden');
						}
					}, 'JSON'
				);
				return false;
			}
		}
	</script>
<?php endif; ?>

<div class="padding bodyhead">
	<p class="pagination crumbs clearfix">
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(), TRUE); ?>" title="Форум Nissan Cedric">Форум</a>
		&raquo;
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'section', 'id' => $section->id)), TRUE); ?>" title="<?php echo HTML::chars($section->name); ?>"><?php echo $section->name; ?></a>
		&raquo;
		<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'cat', 'id' => $cat->id, 'page' => Cookie::get_page('frontend_forum_cat_'.$cat->id.'_page'))), TRUE); ?>" title="<?php echo HTML::chars($cat->name); ?>"><?php echo Text::limit_chars($cat->name, 96); ?></a>
		&raquo;
	</p>
	<h1><?php echo $topic->name; ?></h1>
</div>

<div class="padding">
	<div class="w100 clearfix" style="margin-bottom: -8px;">
		<?php echo $pagination->render(); ?>
		<noindex>
			<p class="pagination forum-buttons fright">
				<?php if ($topic->open != 0): ?><a href="#message_form_start">Ответить</a><?php endif; ?>
				<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'cat', 'id' => $topic->cat_id)), TRUE); ?>#topic_form_start">Создать тему</a>
			</p>
		</noindex>
	</div>
	<table class="forum forum_topic">
		<thead>
			<tr>
				<td class="fsmini">Автор</td>
				<td class="fsmini">Сообщение</td>
			</tr>
		</thead>
		<tbody>
			<?php foreach ($messages as $message): ?>
			<tr id="message<?php echo $message->id; ?>">
				<td class="author fsmini">
					<div class="userpic">
						<?php echo Model_User::html_avatar($message->user); ?>
					</div>
					<div class="userlink">
						<a href="<?php echo URL::site(Route::get('frontend_user')->uri(array(
							'action' => 'view',
							'id' => $message->user->id,
						))); ?>"><?php echo Model_User::nickname($message->user); ?></a>
					</div>
					<div class="userdata fclight">
						<span class="str">Сообщения: <?php echo intval($message->user->forum_total_messages); ?></span>
						<?php if ( ! empty($message->user->country)) echo '<span class="str">Страна: '.$message->user->country.'</span>'; ?>
						<?php if ( ! empty($message->user->city)) echo '<span class="str">Город: '.$message->user->city.'</span>'; ?>
					</div>
				</td>
				<td class="message">
					<?php if ($message->deleted != 1): ?>
						<div class="date fclight fsmini">
							<?php echo Date::format_timezone($message->date); ?>
							<?php if ($topic->open != 0 AND Model_Reg::get('user')->id == $message->user_id): ?>
								<span class="sep">|</span>
								<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array(
									'action' => 'edit',
									'id' => $message->id,
								)), TRUE); ?>#message_form_start" class="action_link">Редактировать</a>
							<?php endif; ?>
							<noindex>
								<div class="actions">
									Сообщение <a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'goto', 'id' => $message->id)), TRUE); ?>" title="Ссылка на это сообщение" onclick="prompt('Прямая ссылка на сообщение #<?php echo $message->id; ?>:', $(this).attr('href')); return false;">#<?php echo $message->id; ?></a>
									<?php if ($topic->open != 0): ?>
										<span class="sep">|</span>
										<a href="">Ответить</a>
									<?php endif; ?>
								</div>
							</noindex>
						</div>
						<div class="msgtext"><?php echo BBCode::toXHTML($message->text); ?></div>
					<?php else: ?>
						<div class="date fclight fsmini">
							<?php echo Date::format_timezone($message->date, NULL, 'Asia/Novosibirsk'); ?>
						</div>
						<div class="msgtext fclight">Сообщение удалено администратором.</div>
					<?php endif; ?>
				</td>
			</tr>
			<?php endforeach; ?>
		</tbody>
	</table>
	<div class="w100 clearfix">
		<?php echo $pagination->render(); ?>
		<noindex>
			<p class="pagination forum-buttons fright">
				<?php if ($topic->open != 0): ?><a href="#message_form_start">Ответить</a><?php endif; ?>
				<a href="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'cat', 'id' => $topic->cat_id)), TRUE); ?>#topic_form_start">Создать тему</a>
			</p>
		</noindex>
	</div>
</div>

<?php if ($topic->open != 0): ?>
	<div id="message_form_start" class="padding">
		<div id="message_form_msgbox"></div>
		<form id="message_form" class="default" action="<?php echo URL::site(Route::get('frontend_forum')->uri(array('action' => 'preview')), TRUE); ?>#message_preview" method="post" onsubmit="return submit_message_form();">
			<fieldset>
				<legend>Добавить сообщение</legend>
				<input type="hidden" name="preview" value="0" />
				<input type="hidden" name="message[section_id]" value="<?php echo $section->id; ?>" />
				<input type="hidden" name="message[cat_id]" value="<?php echo $cat->id; ?>" />
				<input type="hidden" name="message[topic_id]" value="<?php echo $topic->id; ?>" />
				<table class="cols">
					<tr>
						<td class="col col-topic-form-fields">
							<div class="row">
								<div class="field"><textarea id="markItUp" name="message[text]"></textarea></div>
								<script>$('#markItUp').markItUp(markitupSettings);</script>
							</div>
							<div class="row row-button">
								<input type="submit" name="save" value="Предпросмотр" onclick="$('#message_form input[name=preview]').val('1');" />
								<input type="submit" name="save" value="Опубликовать" onclick="$('#message_form input[name=preview]').val('0');" />
								<img id="message_form_ajax" class="ajax_icon" src="/media/img/ajax.gif" alt="" />
								<label class="box fssmall"><input type="checkbox" name="message[subscribe]" value="1" <?php if ($subscribed->id > 0) echo 'checked="checked"'; ?> /> Получать комментарии на e-mail</label>
							</div>
						</td>
					</tr>
				</table>
			</fieldset>
		</form>
	</div>
<?php endif; ?>